[T]
array [T]
[src]
pub fn index(self, value: T): int? {
let i = 0;
let len = self.len();
while (i < len) {
if (self[i] == value) {
return i;
}
i += 1;
}
return nil;
}
Return the index of the given value in int?
.
[src]
pub fn index(self, value: T): int? {
let i = 0;
let len = self.len();
while (i < len) {
if (self[i] == value) {
return i;
}
i += 1;
}
return nil;
}
Return the index of the given value in int?
.
Return the index of the given value in int?
.
If the value is not found, return nil
.
let items = ["foo", "bar", "baz"];
assert_eq items.index("foo"), 0;
assert_eq items.index("bar"), 1;
assert_eq items.index("baz"), 2;
assert_eq items.index("qux"), nil;
let items = [8, 6, 13];
assert_eq items.index(8), 0;
assert_eq items.index(6), 1;
assert_eq items.index(13), 2;
assert_eq items.index(1), nil;
let items = [8.31, 6.211, 13.0];
assert_eq items.index(8.31), 0;
assert_eq items.index(6.211), 1;
assert_eq items.index(13.0), 2;
assert_eq items.index(1.0), nil;
let items = [true, false, true];
assert_eq items.index(true), 0;
assert_eq items.index(false), 1;
assert_eq items.index(true), 0;
[src]
pub fn contains(self, value: T): bool {
for (let a in self) {
if (a == value) {
return true;
}
}
return false;
}
Check a string array if contains the given value.
[src]
pub fn contains(self, value: T): bool {
for (let a in self) {
if (a == value) {
return true;
}
}
return false;
}
Check a string array if contains the given value.
Check a string array if contains the given value.
Examples
let items = ["a", "b", "c"];
assert_eq items.contains("a"), true;
assert_eq items.contains("d"), false;
let items = [1, 2, 3];
assert_eq items.contains(1), true;
assert_eq items.contains(4), false;
let items = [1.0, 2.0, 3.0];
assert_eq items.contains(1.0), true;
assert_eq items.contains(4.0), false;
let items = [true, true];
assert_eq items.contains(true), true;
assert_eq items.contains(false), false;
unique
pub fn unique(self): [T]
[src]
pub fn unique(self): [T] {
let r: [T] = [];
for (let a in self) {
if (!r.contains(a)) {
r.push(a);
}
}
self.clear();
self.append(r);
return self;
}
Unique elements in the array, and modify the array, and return self.
unique
pub fn unique(self): [T]
[src]
pub fn unique(self): [T] {
let r: [T] = [];
for (let a in self) {
if (!r.contains(a)) {
r.push(a);
}
}
self.clear();
self.append(r);
return self;
}
Unique elements in the array, and modify the array, and return self.
Unique elements in the array, and modify the array, and return self.
Examples
let items = ["a", "b", "a", "c"];
assert_eq items.unique(), ["a", "b", "c"];
assert_eq items, ["a", "b", "c"];
let items = [1, 2, 4, 2, 1, 3];
assert_eq items.unique(), [1, 2, 4, 3];
let items = [1.0, 2.0, 2.00, 4.23, 2.1, 1.0, 3.14];
assert_eq items.unique(), [1.0, 2.0, 4.23, 2.1, 3.14];
let items = [true, true, false, true];
assert_eq items.unique(), [true, false];
max
pub fn max(self): T?
[src]
pub fn max(self): T? {
let i = 0;
let max_value: T? = nil;
let len = self.len();
while (i < len) {
if (max_value == nil || self[i] > max_value!) {
max_value = self[i];
}
i += 1;
}
return max_value;
}
Returns the maximum element.
max
pub fn max(self): T?
[src]
pub fn max(self): T? {
let i = 0;
let max_value: T? = nil;
let len = self.len();
while (i < len) {
if (max_value == nil || self[i] > max_value!) {
max_value = self[i];
}
i += 1;
}
return max_value;
}
Returns the maximum element.
Returns the maximum element.
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.max(), 44;
let items = ["a", "b", "c"];
assert_eq items.max(), "c";
let items = [1.3, 0.2, 3.4, -0.1];
assert_eq items.max(), 3.4;
let items = ['a', 'b', 'c'];
assert_eq items.max(), 'c';
min
pub fn min(self): T?
[src]
pub fn min(self): T? {
let i = 0;
let min_value: T? = nil;
let len = self.len();
while (i < len) {
if (min_value == nil || self[i] < min_value!) {
min_value = self[i];
}
i += 1;
}
return min_value;
}
Returns the minimum element.
min
pub fn min(self): T?
[src]
pub fn min(self): T? {
let i = 0;
let min_value: T? = nil;
let len = self.len();
while (i < len) {
if (min_value == nil || self[i] < min_value!) {
min_value = self[i];
}
i += 1;
}
return min_value;
}
Returns the minimum element.
Returns the minimum element.
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.min(), -34;
let items = ["a", "b", "c"];
assert_eq items.min(), "a";
let items = [1.3, 0.2, 3.4, -0.1];
assert_eq items.min(), -0.1;
let items = ['a', 'b', 'c'];
assert_eq items.min(), 'a';
position_max
pub fn position_max(self): int?
[src]
pub fn position_max(self): int? {
let i = 0;
let max_value: T? = nil;
let max_i: int? = nil;
let len = self.len();
while (i < len) {
if (max_value == nil || self[i] > max_value!) {
max_value = self[i];
max_i = i;
}
i += 1;
}
return max_i;
}
Return the position of the maximum element.
position_max
pub fn position_max(self): int?
[src]
pub fn position_max(self): int? {
let i = 0;
let max_value: T? = nil;
let max_i: int? = nil;
let len = self.len();
while (i < len) {
if (max_value == nil || self[i] > max_value!) {
max_value = self[i];
max_i = i;
}
i += 1;
}
return max_i;
}
Return the position of the maximum element.
Return the position of the maximum element.
Examples
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.position_max(), 2;
let items = ["a", "b", "c"];
assert_eq items.position_max(), 2;
let items = [1.3, 0.2, 3.4, -0.1];
assert_eq items.position_max(), 2;
let items = ['a', 'b', 'c'];
assert_eq items.position_max(), 2;
position_min
pub fn position_min(self): int?
[src]
pub fn position_min(self): int? {
let i = 0;
let min_value: T? = nil;
let min_i: int? = nil;
let len = self.len();
while (i < len) {
if (min_value == nil || self[i] < min_value!) {
min_value = self[i];
min_i = i;
}
i += 1;
}
return min_i;
}
Return the position of the minimum element.
position_min
pub fn position_min(self): int?
[src]
pub fn position_min(self): int? {
let i = 0;
let min_value: T? = nil;
let min_i: int? = nil;
let len = self.len();
while (i < len) {
if (min_value == nil || self[i] < min_value!) {
min_value = self[i];
min_i = i;
}
i += 1;
}
return min_i;
}
Return the position of the minimum element.
Return the position of the minimum element.
Examples
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.position_min(), 4;
sum
pub fn sum(self): T
[src]
pub fn sum(self): T {
let sum: T = 0;
for (let a in self) {
sum += a;
}
return sum;
}
Sums the elements.
sum
pub fn sum(self): T
[src]
pub fn sum(self): T {
let sum: T = 0;
for (let a in self) {
sum += a;
}
return sum;
}
Sums the elements.
Sums the elements.
This is only available for arrays of integers and floats.
Examples
let a = [2, 43, 56];
assert_eq a.sum(), 101;
let b = [2.0, 43.0, 56.0];
assert_eq a.sum(), 101;
product
pub fn product(self): T
[src]
pub fn product(self): T {
let product: T = 1;
for (let a in self) {
product *= a;
}
return product;
}
Iterate over the entire array and multiply all the elements.
product
pub fn product(self): T
[src]
pub fn product(self): T {
let product: T = 1;
for (let a in self) {
product *= a;
}
return product;
}
Iterate over the entire array and multiply all the elements.
Iterate over the entire array and multiply all the elements.
This is only available for arrays of integers and floats.
Examples
let a = [2, 43, 56];
assert_eq a.product(), 2 * 43 * 56;
let b = [2.0, 43.0, 56.0];
assert_eq a.product(), 2 * 43 * 56;
sort
pub fn sort(self): [T]
[src]
pub fn sort(self): [T] {
quick_sort(self, 0, self.len() - 1);
return self;
}
Sort elements in the array, modify the array, and return self.
sort
pub fn sort(self): [T]
[src]
pub fn sort(self): [T] {
quick_sort(self, 0, self.len() - 1);
return self;
}
Sort elements in the array, modify the array, and return self.
Sort elements in the array, modify the array, and return self.
Examples
let items = ["c", "a", "b"];
assert_eq items.sort(), ["a", "b", "c"];
assert_eq items, ["a", "b", "c"];
let items = [3, 1, 10];
assert_eq items.sort(), [1, 3, 10];
assert_eq items, [1, 3, 10];
let items = [3.14, 1.0, 10.0];
assert_eq items.sort(), [1.0, 3.14, 10.0];
assert_eq items, [1.0, 3.14, 10.0];
let items = [true, false, false];
assert_eq items.sort(), [false, false, true];
assert_eq items, [false, false, true];
let items = ['c', 'a', 'b'];
assert_eq items.sort(), ['a', 'b', 'c'];
assert_eq items, ['a', 'b', 'c'];
append
pub fn append(self, b: [T])
Appends all the elements to the array.
append
pub fn append(self, b: [T])
Appends all the elements to the array.
Appends all the elements to the array.
Examples
let items = ["a", "b"];
items.append(["c", "d"]);
assert_eq items, ["a", "b", "c", "d"];
concat
pub fn concat(self, b: [T]): [T]
Concatenates two arrays.
concat
pub fn concat(self, b: [T]): [T]
Concatenates two arrays.
Concatenates two arrays.
Examples
let a = [2, 43, 56];
let b = [55, 22, 6];
assert_eq a.concat(b), [2, 43, 56, 55, 22, 6];
filter_map
pub fn filter_map<Q>(self, f: |(T)|: T?): [T]
Creates an array that both filters and maps.
filter_map
pub fn filter_map<Q>(self, f: |(T)|: T?): [T]
Creates an array that both filters and maps.
Creates an array that both filters and maps.
Examples
let items = [2, 43, 56, 7, 23, -245];
let result = items.filter_map(|x| {
if (x > 10) {
return `${x}`;
} else {
return nil;
}
});
assert_eq result, ["43", "56", "23"];
Creates an array which uses a closure to determine if an element should be yielded.
Creates an array which uses a closure to determine if an element should be yielded.
Creates an array which uses a closure to determine if an element should be yielded.
Examples
let items = [2, 43, 56, 7, 23, -245];
let result = items.filter(|x| x > 10);
assert_eq result, [43, 56, 23];
map
pub fn map<Q>(self, f: |(T)|: T): [T]
Returns the element that gives the minimum value with respect to the specified comparison function.
map
pub fn map<Q>(self, f: |(T)|: T): [T]
Returns the element that gives the minimum value with respect to the specified comparison function.
Returns the element that gives the minimum value with respect to the specified comparison function.
Examples
let items = [2, 43, 56];
let result = items.map::<int, string>(|x| `${x}`);
assert_eq result, ["2", "43", "56"];
let items = ["2", "43", "56"];
let result = items.map::<string, string>(|x| `item = ${x}`);
assert_eq result, ["item = 2", "item = 43", "item = 56"];
position_min_by
pub fn position_min_by(self, cmp: |(T, T)|: bool): int?
Returns the position of the the element that gives the minimum value with respect to the specified comparison function.
position_min_by
pub fn position_min_by(self, cmp: |(T, T)|: bool): int?
Returns the position of the the element that gives the minimum value with respect to the specified comparison function.
Returns the position of the the element that gives the minimum value with respect to the specified comparison function.
Examples
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.position_max(), 2;
assert_eq items.position_min(), 4;
assert_eq items.position_max_by(|a, b| a < b), 2;
assert_eq items.position_min_by(|a, b| a < b), 4;
Returns the element that gives the minimum value with respect to the specified comparison function.
Returns the element that gives the minimum value with respect to the specified comparison function.
Returns the element that gives the minimum value with respect to the specified comparison function.
Examples
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.min_by(|a, b| a < b), -34;
position_max_by
pub fn position_max_by(self, cmp: |(T, T)|: bool): int?
Returns the position of the the element that gives the maximum value with respect to the specified comparison function.
position_max_by
pub fn position_max_by(self, cmp: |(T, T)|: bool): int?
Returns the position of the the element that gives the maximum value with respect to the specified comparison function.
Returns the position of the the element that gives the maximum value with respect to the specified comparison function.
Examples
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.position_max(), 2;
assert_eq items.position_min(), 4;
assert_eq items.position_max_by(|a, b| a < b), 2;
assert_eq items.position_min_by(|a, b| a < b), 4;
Returns the element that gives the maximum value with respect to the specified comparison function.
Returns the element that gives the maximum value with respect to the specified comparison function.
Returns the element that gives the maximum value with respect to the specified comparison function.
Examples
let items = [10, 23, 44, 2, -34, -2];
assert_eq items.max_by(|a, b| a < b), 44;
Get a slice from a array.
Get a slice from a array.
Get a slice from a array.
Examples
let items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let result = items.slice(3, 6);
assert_eq result, [4, 5, 6];
items[5] = 77;
assert_eq result, [4, 5, 6];
assert_eq items[5], 77;
clone
pub fn clone(self): [T]
Shallow copy this array.
clone
pub fn clone(self): [T]
Shallow copy this array.
Shallow copy this array.
Examples
let a = [1, 2, 3];
let b = a.clone();
a.clear();
assert_eq a.len(), 0;
assert_eq b.len(), 3;
clear
pub fn clear(self): [T]
Clears the array, removing all values.
clear
pub fn clear(self): [T]
Clears the array, removing all values.
Clears the array, removing all values.
Modifies the array in place, and return self.
Examples
let items = [1, 2, 3];
assert_eq items.clear(), [];
assert_eq items, [];
last
pub fn last(self): T?
Return last element of the array. Or nil
if the array is empty.
last
pub fn last(self): T?
Return last element of the array. Or nil
if the array is empty.
Return last element of the array. Or nil
if the array is empty.
Examples
let items = [1, 2, 3];
assert_eq items.last()!, 3;
let items: [int] = [];
assert_eq items.last(), nil;
first
pub fn first(self): T?
Return first element of the array. Or nil
if the array is empty.
first
pub fn first(self): T?
Return first element of the array. Or nil
if the array is empty.
Return first element of the array. Or nil
if the array is empty.
Examples
let items = [1, 2, 3];
assert_eq items.first()!, 1;
let items: [int] = [];
assert_eq items.first(), nil;
Returns an chunks array over chunk_size
elements of the slice at a time, starting at the beginning of the slice.
Returns an chunks array over chunk_size
elements of the slice at a time, starting at the beginning of the slice.
Returns an chunks array over chunk_size
elements of the slice at a time, starting at the beginning of the slice.
The chunks are slices and do not overlap. If chunk_size
does not divide the length of the slice, then the last chunk will not have length chunk_size
.
Examples
let items = [1, 2, 3, 4, 5];
assert_eq items.chunks(2), [[1, 2], [3, 4], [5]];
Shortens the array
, keeping the first len elements and dropping the rest.
Shortens the array
, keeping the first len elements and dropping the rest.
Shortens the array
, keeping the first len elements and dropping the rest.
Modifies the array in place, and return self.
If len
is greater than the array's current length, this has no effect.
Examples
let items = [1, 2, 3, 4, 5];
assert_eq items.truncate(2), [1, 2];
assert_eq items, [1, 2];
Splits the collection into two at the given index.
Splits the collection into two at the given index.
Splits the collection into two at the given index.
Returns a newly allocated vector containing the elements in the range [at, len).
After the call, the original array
will be left containing the elements [0, at) with its previous capacity unchanged.
Panics if at`` >
len`.
Examples
let items = [1, 2, 3, 4, 5];
let new_items = items.split_off(2);
assert_eq items, [1, 2];
assert_eq new_items, [3, 4, 5];
Resizes the array
in-place so that len
is equal to new_len
.
Resizes the array
in-place so that len
is equal to new_len
.
Resizes the array
in-place so that len
is equal to new_len
.
To modify the array in place, and return self.
If new_len
is greater than len, the array
is extended by the difference, with each additional slot filled with value.
If new_len
is less than len, the array
is simply truncated.
Examples
let items = [1, 2, 3];
assert_eq items.resize(5, 123), [1, 2, 3, 123, 123];
assert_eq items, [1, 2, 3, 123, 123];
let items = [1, 2, 3];
items.resize(2, 123);
assert_eq items, [1, 2];
Sort the array by the given closure, modify the array, and return self.
Sort the array by the given closure, modify the array, and return self.
Sort the array by the given closure, modify the array, and return self.
Examples
let items = [3, 1, 10];
assert_eq items.sort_by(|a, b| {
return a < b;
}), [1, 3, 10];
assert_eq items, [1, 3, 10];
reverse
pub fn reverse(self): [T]
Convert elements into reversed order.
reverse
pub fn reverse(self): [T]
Convert elements into reversed order.
Convert elements into reversed order.
Examples
let items = ["a", "b", "c"];
assert_eq items.reverse(), ["c", "b", "a"];
assert_eq items, ["c", "b", "a"];
let items = [3, 1, 10];
assert_eq items.reverse(), [10, 1, 3];
assert_eq items, [10, 1, 3];
let items = [3.14, 1.0, 10.0];
assert_eq items.reverse(), [10.0, 1.0, 3.14];
assert_eq items, [10.0, 1.0, 3.14];
let items = [true, false, false];
assert_eq items.reverse(), [false, false, true];
assert_eq items, [false, false, true];
contains_by
pub fn contains_by(self, f: |(T)|: bool): bool
Returns true
if the array contains value with specified function.
contains_by
pub fn contains_by(self, f: |(T)|: bool): bool
Returns true
if the array contains value with specified function.
Returns true
if the array contains value with specified function.
Remove the value at the given index.
Remove the value at the given index.
Remove the value at the given index.
Examples
let items = ["a", "b", "c"];
items.remove(1);
assert_eq items.len(), 2;
assert_eq items, ["a", "c"];
Set the value at the given index.
Set the value at the given index.
Set the value at the given index.
Examples
let items = ["a", "b", "c"];
items.set(0, "d");
assert_eq items[0], "d";
items[1] = "e";
assert_eq items[1], "e";
Return the value at the given index.
Return the value at the given index.
Return the value at the given index.
If the index is out of range, return nil
.
Examples
let items = ["a", "b", "c"];
assert_eq items.get(0), "a";
assert_eq items.get(1), "b";
assert_eq items[2], "c";
assert_eq items.get(3), nil;
shift
pub fn shift(self): T?
Pop a value from the front of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
shift
pub fn shift(self): T?
Pop a value from the front of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
Pop a value from the front of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
alias:
pop_front
Examples
let items = ["a", "b", "c"];
let item = items.shift();
// "a"
assert_eq items.shift(), "b";
pop_front
pub fn pop_front(self): T?
Pop a value from the front of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
pop_front
pub fn pop_front(self): T?
Pop a value from the front of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
Pop a value from the front of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
alias:
pop_front
Examples
let items = ["a", "b", "c"];
let item = items.shift();
// "a"
assert_eq items.shift(), "b";
unshift
pub fn unshift(self, value: T)
Push a value to the front of the array.
unshift
pub fn unshift(self, value: T)
Push a value to the front of the array.
Push a value to the front of the array.
alias:
push_front
Examples
let items = ["b", "c"];
items.unshift("a");
assert_eq items, ["a", "b", "c"];
push_front
pub fn push_front(self, value: T)
Push a value to the front of the array.
push_front
pub fn push_front(self, value: T)
Push a value to the front of the array.
Push a value to the front of the array.
alias:
push_front
Examples
let items = ["b", "c"];
items.unshift("a");
assert_eq items, ["a", "b", "c"];
pop
pub fn pop(self): T?
Pop a value from the end of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
pop
pub fn pop(self): T?
Pop a value from the end of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
Pop a value from the end of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
alias:
pop_back
Examples
let items = [1, 2, 3];
assert_eq items.pop(), 3;
assert_eq items, [1, 2];
assert_eq items.pop(), 2;
assert_eq items, [1];
assert_eq items.pop(), 1;
assert_eq items, [];
assert_eq items.pop(), nil;
pop_back
pub fn pop_back(self): T?
Pop a value from the end of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
pop_back
pub fn pop_back(self): T?
Pop a value from the end of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
Pop a value from the end of the array.
Return the popped value and whether the array is empty.
If the array is empty, return nil
.
alias:
pop_back
Examples
let items = [1, 2, 3];
assert_eq items.pop(), 3;
assert_eq items, [1, 2];
assert_eq items.pop(), 2;
assert_eq items, [1];
assert_eq items.pop(), 1;
assert_eq items, [];
assert_eq items.pop(), nil;
push
pub fn push(self, value: T)
Push a value to the end of the array.
push
pub fn push(self, value: T)
Push a value to the end of the array.
Push a value to the end of the array.
alias:
push_back
Examples
let items = [1, 2, 3];
items.push(4);
assert_eq items, [1, 2, 3, 4];
push_back
pub fn push_back(self, value: T)
Push a value to the end of the array.
push_back
pub fn push_back(self, value: T)
Push a value to the end of the array.
Push a value to the end of the array.
alias:
push_back
Examples
let items = [1, 2, 3];
items.push(4);
assert_eq items, [1, 2, 3, 4];
Returns index of value with specified function.
Returns index of value with specified function.
Returns index of value with specified function.
Examples
let items = ["foo", "bar", "baz"];
assert_eq items.index_by(|value| {
return value == "foo";
}), 0;
assert_eq items.index_by(|value| {
return value == "bar";
}), 1;
assert_eq items.index_by(|value| {
return value == "baz";
}), 2;
assert_eq items.index_by(|value| {
return value == "qux";
}), nil;
Return true
if the array is empty.
Return true
if the array is empty.
Return true
if the array is empty.
let items = [1, 2, 3];
items.is_empty(); // false
let items: [int] = [];
items.is_empty(); // true
Return the length of the array.
Return the length of the array.
Return the length of the array.
let items = [1, 2, 3];
assert_eq items.len(), 3;